package gupao.concurrency.threads.aqs;

import gupao.concurrency.threads.Utils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class Consumer implements Runnable{
    private final ProductsContainer<Product> container;
    private final AtomicBoolean stop;
    private final long timeCost;

    public Consumer(ProductsContainer<Product> container, AtomicBoolean stop, long timeCost) {
        this.container = container;
        this.stop = stop;
        this.timeCost = timeCost;
    }

    @Override
    public void run() {
        while(!stop.get()){
            try {
                Product p = container.get();
                if(p == Product.PILL){
                    stop.set(true);
                    log.info("consume finished");
                }else{
                    log.info("consume product: {}, now container size: {}", p, container.getSize());
                }
                Utils.sleepIgnoreExp(timeCost);
            } catch (InterruptedException e) {
            }
        }
    }
}
